<template>
  <div class="div_padding">
    <el-form
      :inline="true"
      :model="formInline"
      ref="formInline"
      class="demo-form-inline"
      size="small"
    >
      <el-form-item label="地区名称：" prop="name">
        <el-input v-model="formInline.name" placeholder="地区名称"></el-input>
      </el-form-item>
      <el-form-item label="编码：" prop="code">
        <el-input v-model="formInline.code" placeholder="编码"></el-input>
      </el-form-item>
      <el-form-item label="状态：" prop="enable">
        <el-select v-model="formInline.enable" placeholder="状态">
          <el-option label="正常" value=True></el-option>
          <el-option label="停用" value=False></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" v-permission="'Search'" type="primary" @click="filterData"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" plain @click="resetForm('formInline')"
          >重置</el-button
        >
        <el-button icon="el-icon-plus" v-permission="'Create'" type="success" @click="onSubmit"
          >添加地域</el-button
        >
        <Insert
          ref="insert"
          @reloadTable="reloadTable"
        ></Insert>
        <Edit
          ref="edit"
          v-bind:areaData="tableData"
          @reloadTable="reloadTable"
        ></Edit>
      </el-form-item>
      <el-button
        style="float: right"
        icon="el-icon-refresh-right"
        circle
        @click="reloadTable"
      ></el-button>
    </el-form>
      <el-table
        :data="tableData"
        size="mini"
        height="72vh"
        v-loading="loading"
        empty-text="查询结果为空!"
        element-loading-text="数据加载中..."
        element-loading-spinner="el-icon-loading"
        style="width: 100%; float: right"
        row-key="code"
        lazy
        border
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column
          prop="id"
          label="ID"
          v-if="showId"
          align="center"
          width="180"
        >
        </el-table-column>
        <el-table-column
          prop="pcode"
          label="pcode"
          v-if="showId"
          align="center"
          width="180"
        >
        </el-table-column>
        <el-table-column type="index" label="序号" align="center" width="80">
        </el-table-column>
        <el-table-column
          prop="name"
          label="地区名称"
          align="center"
          width="180"
        >
        </el-table-column>
        <el-table-column
          prop="code"
          label="地区编码"
          align="center"
          width="100"
        >
        </el-table-column>
        <el-table-column prop="pinyin" label="拼音" align="center" width="120">
        </el-table-column>
        <el-table-column prop="level" label="层级" align="center" width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.level == 1"
              >省</el-tag
            >
            <el-tag v-if="scope.row.level == 2" type="success"
              >市</el-tag
            >
            <el-tag v-if="scope.row.level == 3" type="warning"
              >区/县</el-tag
            >
            <el-tag v-if="scope.row.level == 4" type="info"
              >乡/镇</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="initials"
          label="首字母"
          align="center"
          width="100"
        >
        </el-table-column>
        <el-table-column
          prop="enable"
          width="100"
          align="center"
          label="显示状态"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.enable == true" style="color: #67c23a"
              >正常</span
            >
            <span v-if="scope.row.enable == false" style="color: #909399"
              >停用</span
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="update_datetime"
          label="更新时间"
          align="center"
          width="180"
        >
        </el-table-column>
        <el-table-column
          prop="create_datetime"
          label="创建时间"
          align="center"
          width="180"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="250"
          prop="parent"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              type="primary"
              v-permission="'Update'"
              size="mini"
              @click="onEdit(scope.$index, scope.row)"
              icon="el-icon-edit"
              plain
              >修改</el-button
            >
            <el-button
              type="danger"
              v-permission="'Delete'"
              size="mini"
              icon="el-icon-delete"
              plain
              @click="onDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin: 5px; float: left">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="cur_page"
          :page-sizes="[20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
  </div>
</template>
<script>
import { getAreasData, delAreaData } from "@/api/areas";
import { successMsg, errorMsg } from "@/utils/tools";
import Insert from '../areas/insert'
import Edit from '../areas/edit'
export default {
  name: "Areas",
  components:{
    Insert,
    Edit
  },
  data() {
    return {
      showId: false,
      loading: true,
      tableData: [],
      total: 0,
      cur_page: 1,
      pageSize: 20,
      formInline: {
        name: "",
        code: "",
        enable: "",
        page:1,
        limit:50
      },
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      getAreasData({page:this.cur_page,limit:this.pageSize}).then((res) => {
        this.tableData = res.data.data
        this.total = res.data.total;
        this.loading = false;
      });
    },
    onSubmit() {
      this.$refs.insert.dialogFormVisible = true;
    },
    onEdit(index,row){
      console.log(row)
      this.$refs.edit.dialogFormVisible = true
      this.$refs.edit.formInline = Object.assign({}, row);//formInline，将row的数据复制到editForm对象中，实现的是浅拷贝
    },
    onDelete(row) {
      this.$confirm("此操作将真实删除该地域,谨慎操作!是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delAreaData(row.id).then((res) => {
            if (res.code === 2000) {
              successMsg(res.msg);
              this.reloadTable();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // load(tree, treeNode, resolve){
    //   setTimeout(()=>{
    //     resolve(()=>{
    //       this.getAreasData({'pcode':})
    //     })
    //   },10000)
    // },
    reloadTable() {
      this.loading = true;
      this.fetchData();
    },
    filterData() {
      let params = {};
      if (this.formInline.name) {
        params.name = this.formInline.name;
      }
      if (this.formInline.code) {
        params.code = this.formInline.code;
      }
      if (this.formInline.enable) {
        params.enable = this.formInline.enable;
      }
       params.page=this.cur_page
       params.limit=this.pageSize
      this.loading = true;
      getAreasData(params).then((res) => {
        this.tableData = res.data.data
        this.total = res.data.total;
        this.loading = false;
      });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.loading = true;
      this.filterData();
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.loading = true;
      this.filterData();
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.reloadTable();
    },
  },
};
</script>
<style lang="scss" scoped>
.div_padding {
  padding: 10px 10px;
}
</style>
